<?php
function dd($a){
    var_dump($a);die;
}
/**
 * 求两个日期之间相差的完整天数
 * @param string $day1
 * @param string $day2
 * @return false|float
 */
function getDayNum($day1, $day2)
{
    $second1 = strtotime($day1);
    $second2 = strtotime($day2);

    if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
    }
    return floor(($second1 - $second2) / 86400);
}

/**
 * @param $start
 * @param $number
 * @param $type
 * @return string
 */
function getEndDay($start,$number,$type): string
{
    switch ($type){
        case 1:
            return getPurMonth($start, $number);
        case 2:
            $number = $number*3;
            return getPurMonth($start, $number);
        case 3:
            return date('Y-m-d',strtotime("{$start}+{$number}years"));
        default:
            return date('Y-m-d',strtotime("{$start}+{$number}days"));
    }
}

/**
 * @param $date
 * @param $number
 * @return string
 * 获取指定日期的后N个月
 */
function getPurMonth($date,$number): string
{
    $arr = getdate(strtotime($date));
    $years = floor($number/12);
    $months = $number%12;
    $year = $arr['year'] +$years;
    $month = $arr['mon']+$months;

    if($month > 12){
        $year++;
        $month = $arr['mon']+$months-12;
    }
    if($month < 10){
        $month = '0'.$month;
    }
    $expireMonthDay = date('t',strtotime($year.'-'.$month));
    $day = $arr['mday'];
    if($expireMonthDay<$day){
        $day = $expireMonthDay;
    }
    if($day < 10){
        $day = '0'.$day;
    }
    return $year.'-'.$month.'-'.$day;
}

/**
 * @param $date1
 * @param $date2
 * @param string $tags
 * @return float|int
 */
function getMonthNum( $date1, $date2, $tags='-' ){
    $date1 = explode($tags,$date1);
    $date2 = explode($tags,$date2);
    return abs(($date1[0] - $date2[0]) * 12 + ($date1[1] - $date2[1]));
}

/**
 * @param $time1
 * @param $time2
 * @return bool
 * 判断两个给定日期是否在同一月
 */
function isSameMonth($time1, $time2)
{
    $m1 = date('Ym', strtotime($time1));
    $m2 = date('Ym', strtotime($time2));
    // 必须判断年份，不然2019-8和2020-8会被认为同一月
    if ($m1 == $m2) {
        return true;
    }
    return false;
}

/**
 * @param $date
 * @return false|string
 */
function getLastMonth($date){
    $timestamp=strtotime($date);
    $month=date('Y-m',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
    return $month;
}